package biolabs.model.division

import biolabs.model.growth.GrowthDelegate
import biolabs.space.ContinuousPosition
import biolabs.space.DiscretePosition
import biolabs.space.GridShapedObject

import scala.collection.JavaConversions._

trait LastGrowthDivisionPlaneDeterminator[
	P <: DiscretePosition[P, C],
	C <: ContinuousPosition[C, P]
]
extends DivisionPlaneDeterminator[C]
{
self: GridShapedObject[P, C] =>

	private var lastGrowth: P = null.asInstanceOf[P]

	override def getDivisionPlaneNormal(): C = {
		if(lastGrowth != null) {
			val lg = lastGrowth.continuous()
			val cg = getShape().getCenterOfGravity()
			if(!lg.isEqualTo(cg))
				return lg.minus(cg)
		}
		return getShape().getElements().head.one.continuous
	}
	
	def handleGrowth(gd: GrowthDelegate[P]) {
		if(!gd.elements.isEmpty)
			lastGrowth = gd.elements.last
	}
}